package api

import (
	"github.com/labstack/echo/v4"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/db/mysql/models/models_main"
	"longmen/server/pkg/grpc_client"
	pb "longmen/server/pkg/protobuf"
)

// @Summary 获取签到信息
// @Description 获取签到信息
// @Accept  json
// @Produce  json
// @Tags checkin
// @Success 200 {object} protobuf.CheckinInfoResp "result"
// @Router /api/checkin/getCheckinInfo [POST]
func (base *Controller) GetCheckinInfo(c echo.Context) error {
	user, err := base.GetUserByCtx(c)
	if err != nil {
		user = &models_main.User{Id: 0}
	}
	ctx, cancel := base.GrpcWithTimeout()
	defer cancel()
	resp, err := grpc_client.GrpcTaskServer.GetCheckinInfo(ctx, &pb.UidReq{
		Uid: uint64(user.Id),
	})
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}

	return util.BuildSuccess(c, resp, "")
}

// @Summary 今日签到
// @Description 今日签到
// @Accept  json
// @Produce  json
// @Tags checkin
// @Success 200 {object} protobuf.CheckinResp "result"
// @Router /api/checkin/checkinToday [POST]
func (base *Controller) CheckinToday(c echo.Context) error {
	user, err := base.GetUserByCtx(c)
	if err != nil {
		return util.BuildLoginTimeOut(c, err)
	}
	ctx, cancel := base.GrpcWithTimeout()
	defer cancel()
	resp, err := grpc_client.GrpcTaskServer.CheckinToday(ctx, &pb.UidReq{
		Uid: uint64(user.Id),
	})
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	return util.BuildSuccess(c, resp, "")
}

// @Summary 获取签到历史
// @Description 获取签到历史
// @Accept  json
// @Produce  json
// @Tags checkin
// @Success 200 {object} protobuf.CheckinHistoryResp "result"
// @Router /api/checkin/getCheckinHistory [POST]
func (base *Controller) GetCheckinHistory(c echo.Context) error {
	user, err := base.GetUserByCtx(c)
	if err != nil {
		return util.BuildLoginTimeOut(c, err)
	}
	ctx, cancel := base.GrpcWithTimeout()
	defer cancel()
	resp, err := grpc_client.GrpcTaskServer.GetCheckinHistory(ctx, &pb.UidReq{
		Uid: uint64(user.Id),
	})
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	return util.BuildSuccess(c, resp, "")
}
